/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package Reports.Matcher;

import ResultEditor.Annotations.Annotation;
import ResultEditor.Annotations.Article;
import java.util.Vector;

/**
 *  find same span matcher 
 *
 * @author leng
 */
public class SpanMatcher {

    /**vector used to record find*/
    private Vector<Anns> SSAnns = new Vector<Anns>();

    /**record processed annotation to current article*/
    private Vector<Integer> processedannotation =  new Vector<Integer>();


    /**call this function to start analysis to all annotations to find
     * annotations with matched spans.
     */
    public void analysis(){
        try{

            // ##1## remove old data
            clear();

            // ##2## get all annotations
            ResultEditor.Annotations.Depot depot = new ResultEditor.Annotations.Depot();
            Vector<Article> dep = depot.getDepot();

            if((dep==null)||(dep.size()<1)){
                System.out.println("[report system] error - 1101121107:: no annotations in memory.");
                return;
            }


            //##3## go over all article for each annotator
            AnnotatorRegister AR = new AnnotatorRegister();
            for(int i=0;i<AR.size(); i++){
                String annotator = AR.getAnnotatorByIndex(i);
                if (annotator==null)
                    continue;
                if (annotator.trim().length()<1)
                    continue;

                // prepare
                this.processedannotation.clear();

                // check 
                checkHisAnnotations(annotator, dep);
            }
        }catch(Exception ex){
            System.out.println("[report system] error 1101121222:: fail to analysize" +
                    " annotations and generate span matcher report!");
        }
        

    }

    private void clear(){
        SSAnns.clear();
    }

    /**check annotations for annotator.*/
    private void checkHisAnnotations(String annotator, Vector<Article> depot)
    {
        if((annotator==null)||(annotator.trim().length()<1)){
            System.out.println("[report system] error 1101121313 - invalid input!!!");
            return;
        }

        if(depot==null){
            System.out.println("[report system] error 1101121314 - invalid input!!!");
            return;
        }

        try{
        
            for(Article article : depot){
                if(article==null)
                    continue;
                if(article.annotations == null)
                    continue;

                // ##1## go over all annotations to each article
                int size = article.annotations.size();
                for(int i=0; i< size; i++ ){
                    Annotation ann = article.annotations.get(i);
                    if (ann==null)
                        continue;

                    // find annotation belong to this annotator
                    if (ann.annotator.trim().compareTo(annotator.trim())==0){
                        

                        // prepare
                        Vector<Annotation> raw = new Vector<Annotation>();
                        this.processedannotation.add(ann.uniqueIndex);
                        raw.add(ann);
                        Vector<Annotation> samespan = new Vector<Annotation>();
                        Vector<Annotation> differentspan = new Vector<Annotation>();

                        // there may be multiple comparison raw object
                        compareWithBrothers(raw, annotator, ann, article);

                        // compare this annotation with others
                        if(raw.size()==1)
                            compareWithOthers(samespan, differentspan, annotator, ann, article);
                    }
                }

            }
        }catch(Exception ex){
            System.out.println("[report system] error 1101121315:: fail to " +
                    "compare annotations for span matching!");
        }
    }
    


    /**to given annotation, compare it with its bothers to find same spans
     * with can be consider as comparing raw.*/
    private void compareWithBrothers(Vector<Annotation> _raw, String annotator,
            Annotation annotation, Article article)
    {
        
        if((annotator==null)||(annotator.trim().length()<1))
            return;
        if (annotation==null)
            return;
        if ((article==null)||(article.annotations==null))
            return;

        try{

            int size = article.annotations.size();
            for(int i=0; i<size; i++){
                Annotation thisannotation = article.annotations.get(i);
                if (thisannotation==null)
                    continue;
                String thisannotator = thisannotation.annotator;

                // make sure these two annotations belong to same annotator
                if( thisannotator.trim().compareTo(annotator.trim())== 0)
                    continue;

                // make sure they are not same annotation
                if (thisannotation.uniqueIndex == annotation.uniqueIndex)
                    continue;

                // @@@@ if find same span, recorded
                if( ( thisannotation.spanstart== annotation.spanstart )
                    &&(annotation.spanend == thisannotation.spanend)){
                    
                    // record as analysised
                    this.processedannotation.add(thisannotation.uniqueIndex);
                    _raw.add(thisannotation);
                }
            }

        }catch(Exception ex){
            System.out.println("[report system] error 1101121319:: fail to check spans.\n"
                    + ex.getMessage() );
        }

    }

    
    /**to given annotation, compare it with others to find same spans or overlapped spans*/
    private void compareWithOthers(Vector<Annotation> _samespan, Vector<Annotation>
            _differentspan, String annotator, Annotation RAWAnnotation,
            Article article)
    {
        if((annotator==null)||(annotator.trim().length()<1))
            return;
        if (RAWAnnotation==null)
            return;
        if ((article==null)||(article.annotations==null))
            return;

        try{

            int size = article.annotations.size();
            for(int i=0; i<size; i++){
                Annotation thisannotation = article.annotations.get(i);
                if (thisannotation==null)
                    continue;
                String thisannotator = thisannotation.annotator;
                
                //
                if( thisannotator.trim().compareTo(annotator.trim())!= 0)
                    continue;

                int thisstart = thisannotation.spanstart, thisend = thisannotation.spanend;
                int start = RAWAnnotation.spanstart, end = RAWAnnotation.spanend;

                // same span find
                if((thisstart==start)&&(thisend==end)){
                    _samespan.add(thisannotation);
                }else{
                    // search for overlapped span - also means not matched spans
                    
                }


                

            }
            
        }catch(Exception ex){
            System.out.println("[report system] error 1101121319:: fail to check spans.\n"
                    + ex.getMessage() );
        }
        
    }
}



class Anns{
    Vector<Annotation> anns_with_samespan = new Vector<Annotation>();
}
